package correct.day16;

public class Test1 {
    public static String formatString1(String A, int n, char[] arg, int m) {
        StringBuffer ret = new StringBuffer();
        char[] s = A.toCharArray();
        int j = 0;
        for (int i = 0; i < n; i++) {
            if (s[i] != '%') {
                ret.append(s[i]);
            } else {
                if (i + 1 < n && s[i + 1] == 's') {
                    ret.append(arg[j++]);
                    i++;
                } else {
                    ret.append(s[i]);
                }
            }
        }
        while (j < m) {
            ret.append(arg[j++]);
        }
        return ret.toString();
    }

    public static String formatString(String A, int n, char[] arg, int m) {
        // write code here
        int j = 0;
        int k = 0;
        StringBuffer stringBuffer = new StringBuffer();
        while (j < n) {
            // if(j >= n || k >= m) {
            //     break;
            // }
            if (j < n && A.charAt(j) == '%') {
                stringBuffer.append(arg[k]);
                j += 2;
                k++;
            } else {
                stringBuffer.append(A.charAt(j));
                j++;
            }
        }
        while (k < m) {
            stringBuffer.append(arg[k]);
            k++;
        }
        return stringBuffer.toString();
    }

    public static void main(String[] args) {
        // "A%sC%sE",7,[B,D,F],3
        char[] arr = {'B', 'D', 'F'};
        System.out.println(formatString("A%sC%sE", 7, arr, 3));
    }
}
